#! /usr/bin/env python
# coding=utf-8

import os
from Bio import SeqIO
import argparse
import sys
from argparse import RawTextHelpFormatter


parser = argparse.ArgumentParser(
    description='''
    统计基因条码 长度过滤和demultiplex之后 制定标本对应的reads数
    用法:
    count_read_number_after_filter_barcode_reads.py -i specimen_lista -p cut_len -o reads_count.tsv
    ''',formatter_class=RawTextHelpFormatter)


parser.add_argument('-i',
                help='specimen_lista, 目标标本文件')
parser.add_argument('-p',
                help='cut_len 过滤之后的路径 每个文件代表一个')

parser.add_argument('-o',
                help='输出的tsv文件')


args = parser.parse_args()


if not args.i or not args.o or not args.p:
    parser.print_help()
    sys.exit()





infile = args.i
inpath = args.p

outfile = args.o

outfile = open(outfile,'w')

outfile.write('specimen\treads_number\n')


specimen_lista = []

with open(infile) as fila:
	for i in fila:
		specimen_lista.append(i.strip())

print('expect %s specimen' % len(specimen_lista) )

z = 0
o = 0
for i in os.listdir(inpath):
	k = i.split('.')[0]
	if k in specimen_lista:
		o += 1
		n = 0
		for j in SeqIO.parse(inpath+'/'+i,'fastq'):
			n += 1
			z += 1
		outfile.write(k+'\t'+str(n)+'\n')
	
print('obtain are %s reads in %s specimen' % (z,o))

outfile.close()